'use strict';
app.controller('sellLevelCtrl',['$scope','$modal','utilService','sellLevelService','toaster','$log',
    function($scope,$modal,utilService,sellLevelService,toaster,$log){

    $scope.sellLevel={};

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'sell/level',
            params:$scope.sellLevel
        }).then(function(result){
        });

    }

    $scope.query();


    $scope.del = function(id){
        var param = {
            id:id
        };

        // query data.
        openModal("你是否要删除该分润等级？");

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除分润等级",
                controller:"sellLevelDeleteCtrl",
                message:msg,
                data:param
            });
        }
    }
    //增加
    $scope.openAdd=function(){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/systemSetting/distribution_add.html',
            controller: 'distributionAddCtrl',
            scope:$scope,
            size:'sm'
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    //修改
    $scope.openEdit=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/systemSetting/distribution_edit.html',
            controller: 'distributionEditCtrl',
            scope:$scope,
            size:'sm',
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

//添加
app.controller('distributionAddCtrl',['$scope','$modalInstance','sellLevelService','toaster',
    function($scope,$modalInstance,sellLevelService,toaster){
    $scope.AllowToDelete=true;
    $scope.sellLevel={};

    $scope.findMax=function(){
        sellLevelService.findMax($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.sellLevel=result.data.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.findMax();

    $scope.ok=function(){
        if(!$scope.AllowToDelete){
            return;
        }
        $scope.AllowToDelete=false;

        sellLevelService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    $scope.AllowToDelete=true;
                    break;
                default :
                    $modalInstance.dismiss('error');
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

//删除
app.controller('sellLevelDeleteCtrl',['$scope','$modalInstance','sellLevelService','toaster','modalData',
    function($scope,$modalInstance,sellLevelService,toaster,modalData){
        $scope.modal = modalData;
        $scope.isDelModal=true;
        $scope.sellLevelId=modalData.data.id;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            sellLevelService.delete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.query();
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    case 688:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);

//修改
app.controller('distributionEditCtrl',['$scope','utilService','sellLevelService','toaster','data','$modalInstance',
    function($scope,utilService,sellLevelService,toaster,data,$modalInstance){
    $scope.sellLevel={};
    $scope.sellLevelId = data.id;
    $scope.isDelModal=true;

    $scope.findById=function(){
        sellLevelService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.sellLevel=result.data.data;
                    break;
                case 689:
                    toaster.pop('waiting', "提示", result.data.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    $scope.findById();

    //确认修改
    $scope.ok = function(){
        if(!$scope.isDelModal){
            return;
        }
        $scope.isDelModal = false;
        sellLevelService.update($scope).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.dismiss('success');
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 689:
                    toaster.pop('waiting', "提示", result.data.message);
                    $scope.isDelModal=true;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);
